package com.example.vulnerableapplication.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class PickerActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startActivityForResult(new Intent(Intent.ACTION_PICK), 1337);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode != 1337 || resultCode != -1 || data == null || data.getData() == null) {
            return;
        }
        Uri pickedUri = data.getData();
        File cacheFile = new File(getExternalCacheDir(), "temp");
        copy(pickedUri, cacheFile);

        // the file is then processed in some way
    }
    private void copy(Uri uri, File toFile) {
        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);
            OutputStream outputStream = new FileOutputStream(toFile);
            copy(inputStream, outputStream);
        }
        catch (Throwable th) {
            // error handling
        }
    }
    public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
        byte[] bArr = new byte[65536];
        while (true) {
            int read = inputStream.read(bArr);
            if (read == -1) {
                break;
            }
            outputStream.write(bArr, 0, read);
        }
    }
}
